package cn.imnaer.rest.controller;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.BeanUtils;

import cn.imnaer.common.factory.JacksonUtil;
import cn.imnaer.service.commom.exception.UserAlreadyHasException;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.persistence.model.User;
import cn.imnaer.service.rest.resquest.model.UserLoginParams;
import cn.imnaer.service.rest.resquest.model.UserRegisterParams;
import cn.imnaer.service.rest.userinfo.UserComponentRestApi;
import cn.imnaer.service.rest.view.model.RecommendView;
import cn.imnaer.service.rest.view.model.SimpleResponse;
import cn.imnaer.service.rest.view.model.UserView;
import cn.imnaer.service.rpc.scenic.impl.ScenicComponentRpcApiImpl;
import cn.imnaer.service.rpc.userinfo.impl.UserComponentRpcApiImpl;

@WebService
public class UserComponentController implements UserComponentRestApi {
	public SimpleResponse simpleResponse;
	public UserComponentRpcApiImpl userComponentRpcApiImpl;

	public UserComponentController() {
		userComponentRpcApiImpl = new UserComponentRpcApiImpl();
		simpleResponse = new SimpleResponse();
	}

	public String login(String userInfo) {
		UserLoginParams loginUser = JacksonUtil.jsonToBean(userInfo, UserLoginParams.class);
		if (loginUser == null) {
			return this.simpleResponse.jsonException();
		}
		User user = userComponentRpcApiImpl.login(loginUser);
		if (user != null) {
			return this.simpleResponse.success(UserView.valueOf(user));
		}
		return this.simpleResponse.toString(SimpleResponse.ERROR_PASSWORD, "");
	}

	public String register(String registerParamsJson) {
		UserView userView = new UserView();
		UserRegisterParams registerParams = UserRegisterParams.parseToModel(registerParamsJson);
		if (registerParams == null) {
			return simpleResponse.jsonException();
		}
		User user = null;
		try {
			user = userComponentRpcApiImpl.createUser(registerParams);
			userView = UserView.valueOf(user);
		} catch (UserAlreadyHasException e) {
			return simpleResponse.toString(SimpleResponse.USER_HAS, "");
		}
		return simpleResponse.success(userView);
	}

	@Override
	public String getUserInfo(String userId) {
		User user = this.userComponentRpcApiImpl.getUserInfo(userId);
		List<RecommendView> recommendViews = new ArrayList<>();
		List<Scenic> scenics = new ScenicComponentRpcApiImpl().getMyContributeScenics(userId);
		for (Scenic scenic : scenics) {
			RecommendView recommendView = new RecommendView();
			recommendView.setRecommendId(scenic.getScenicId());
			recommendView.setImage(scenic.getImages());
			recommendView.setName(scenic.getScenicName());
			recommendView.setType0(scenic.getCity().getCityName());
			recommendView.setType1(scenic.getCity().getStateName());
			recommendViews.add(recommendView);
		}
		UserView userView = new UserView();
		BeanUtils.copyProperties(user, userView);
		userView.setContributionScenics(recommendViews);
		return this.simpleResponse.success(userView);
	}

	@Override
	public String settingUserInfo(String settingUserInfoJson) {
		
		return null;
	}

}
